//
//  StoresShopcartView.m
//  TerritoryShopkeeper
//
//  Created by 李小龙 on 2018/3/15.
//  Copyright © 2018年 ZDQK. All rights reserved.
//

#import "StoresShopcartView.h"
#import "StoresShopcartTableViewCell.h"

#define RowHeight 50

@interface StoresShopcartView ()<UITableViewDelegate,UITableViewDataSource>

- (void)createUI;
@end

@implementation StoresShopcartView

+ (instancetype)createShopcartView:(NSMutableArray *)cartArray {
    StoresShopcartView *cartView = [[StoresShopcartView alloc] initWithFrame:Screen];
    cartView.tableArray = cartArray;
    
    [cartView createUI];
    
    return cartView;
}

- (void)setCartArrayWith:(NSMutableArray *)array isShow:(BOOL)isShow {
    self.tableArray = array;
    CGFloat tableH = (self.tableArray.count+1)*RowHeight;
    tableH = tableH > (ScreenHeight-50)*0.75 ? (ScreenHeight-50)*0.75 : tableH;
    self.tableView.height = tableH;
    if (!isShow) {
        self.tableView.top = self.height - self.tableView.height - 50;
    }
    [self.tableView reloadData];
}
static NSString *storesShopcartIdentifier = @"storesShopcartIdentifier";
- (void)createUI {
    // 半透明背景
    self.backView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, ScreenWidth, ScreenHeight)];
    [self.backView setBackgroundColor:[UIColor colorWithWhite:0 alpha:0.3]];
    
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapAction:)];
    [self.backView addGestureRecognizer:tap];
    [self addSubview:self.backView];
    
    // tableView
    CGFloat tableH = (self.tableArray.count+1)*RowHeight;
    tableH = tableH > (ScreenHeight-50)*0.75 ? (ScreenHeight-50)*0.75 : tableH;
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, ScreenHeight, ScreenWidth, tableH) style:UITableViewStylePlain];
    [self.tableView setSeparatorInset:UIEdgeInsetsMake(0, 15, 0, 15)];
    [self.tableView setSeparatorColor:[UIColor grayDDColor]];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    [self.tableView registerNib:[UINib nibWithNibName:@"StoresShopcartTableViewCell" bundle:[NSBundle mainBundle]] forCellReuseIdentifier:storesShopcartIdentifier];
    [self addSubview:self.tableView];
    
//    UIWindow *window = [UIApplication sharedApplication].keyWindow;
//    [window addSubview:self];
    [self setHidden:YES];
}
- (void)showView {
    [self setHidden:NO];
    
    [UIView animateWithDuration:0.3 animations:^{
        [self.backView setAlpha:1];
        self.tableView.top = self.height - self.tableView.height - 50;
    } completion:^(BOOL finished) {
        
    }];
}
- (void)hiddenView {
    [self endEditing:YES];
    [UIView animateWithDuration:0.3 animations:^{
        [self.backView setAlpha:0];
        self.tableView.top = ScreenHeight;
    } completion:^(BOOL finished) {
        [self setHidden:YES];
    }];
}
- (void)tapAction:(UIGestureRecognizer *)sender {
    [self hiddenView];
}
#pragma mark - UITableViewDataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.tableArray.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    StoresShopcartTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:storesShopcartIdentifier forIndexPath:indexPath];
    
    StoresGoodModel *model = self.tableArray[indexPath.row];
    if (TextIsNotNil(model.selectAttribute)) {
        NSString *title = [NSString stringWithFormat:@"%@\n%@",model.gname,model.selectAttribute];
        NSMutableAttributedString *attStr = [[NSMutableAttributedString alloc] initWithString:title];
        [attStr setAttributes:@{NSFontAttributeName:TextPingFangFontSize(16),NSForegroundColorAttributeName:[UIColor blackTextColor]} range:[title rangeOfString:model.gname]];
        [attStr setAttributes:@{NSFontAttributeName:TextPingFangFontSize(12),NSForegroundColorAttributeName:[UIColor grayTextColor]} range:[title rangeOfString:model.selectAttribute]];
        [cell.nameLabel setAttributedText:attStr];
    } else {
        [cell.nameLabel setText:model.gname];
    }
    [cell.priceLabel setText:[NSString stringWithFormat:@"¥%@",model.gprice]];
    [cell.countTF setText:model.gcount];
    
    __block typeof(self) blockSelf = self;
    cell.block = ^(NSInteger type) {
        if (type == 0) {
            // 加1
            GoodsAttributePriceModel *priceModel;
            for (GoodsAttributePriceModel *tempModel in model.goodsattribute) {
                if ([model.gaid isEqualToString:tempModel.gaid]) {
                    priceModel = tempModel;
                    break;
                }
            }
            if (priceModel && model.gcount.integerValue < priceModel.gastock.integerValue) {
                model.gcount = [NSString stringWithFormat:@"%ld",model.gcount.integerValue+1];
                [tableView reloadData];
                
                if (blockSelf.ShopcartBlock) {
                    blockSelf.ShopcartBlock(type, model);
                }
            }
        } else {
            // 减1
            if ((model.gcount.integerValue > 0)) {
                model.gcount = [NSString stringWithFormat:@"%ld",model.gcount.integerValue-1];
                if ([model.gcount isEqualToString:@"0"]) {
                    [blockSelf.tableArray removeObject:model];
                    [blockSelf setCartArrayWith:blockSelf.tableArray isShow:NO];
                }
                [tableView reloadData];
                
                if (blockSelf.ShopcartBlock) {
                    blockSelf.ShopcartBlock(type, model);
                }
            }
        }
    };
    
    return cell;
}
#pragma mark - UITableViewDelegate
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return RowHeight;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return RowHeight;
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, ScreenWidth, RowHeight)];
    [view setBackgroundColor:[UIColor whiteColor]];
    
    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, ScreenWidth, RowHeight)];
    [label setBackgroundColor:[UIColor whiteColor]];
    [label setText:@"购物车"];
    [label setFont:TextPingFangFontSize(16)];
    [label setTextColor:[UIColor blackTextColor]];
    [label setTextAlignment:NSTextAlignmentCenter];
    [view addSubview:label];
    
    UIView *lineView = [[UIView alloc] initWithFrame:CGRectMake(0, view.height-0.5, view.width, 0.5)];
    [lineView setBackgroundColor:[UIColor grayDDColor]];
    [view addSubview:lineView];
    
    return view;
}
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
